package other.mvvm

import com.android.ide.common.symbols.parseManifest
import com.android.tools.idea.wizard.template.ModuleTemplateData
import com.android.tools.idea.wizard.template.RecipeExecutor
import other.mvvm.layout.mvvmActivityXml
import other.mvvm.manifest.mvvmActivityManifestXml
import other.mvvm.src.mvvmActivityKt
import other.mvvm.src.mvvmModelKt
import other.mvvm.src.mvvmViewModelKt

fun RecipeExecutor.mvvmActivityRecipe(
        moduleData: ModuleTemplateData,
        activityClass: String,
        layoutName: String,
        packageName: String
) {
    val (projectData, srcOut, resOut, manifestDir) = moduleData
    val ktOrJavaExt = projectData.language.extension
    //判断在manifest中是否存在launcherActivity
    val isNewModule = parseManifest(manifestDir.resolve("AndroidManifest.xml"))?.launcherActivity == null
    //manifest文件中追加信息
    mergeXml(mvvmActivityManifestXml(projectData.applicationPackage, activityClass, layoutName, packageName, isNewModule), manifestDir.resolve("AndroidManifest.xml"))
    //保存activity
    save(mvvmActivityKt(projectData.applicationPackage, activityClass, layoutName, packageName), srcOut.resolve("${activityClass.toLowerCase()}/${activityClass}Activity.${ktOrJavaExt}"))
    //保存xml
    save(mvvmActivityXml(projectData.applicationPackage, activityClass, layoutName, packageName), resOut.resolve("layout/${layoutName}.xml"))
    //保存viewmodel
    save(mvvmViewModelKt(projectData.applicationPackage, activityClass, layoutName, packageName), srcOut.resolve("${activityClass.toLowerCase()}/${activityClass}ViewModel.${ktOrJavaExt}"))
    //保存model
    save(mvvmModelKt(projectData.applicationPackage, activityClass, layoutName, packageName), srcOut.resolve("${activityClass.toLowerCase()}/${activityClass}Model.${ktOrJavaExt}"))
}